package com.zsyh.jiujiu.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ActivityUtils;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.example.zyh.toolslib.base.BaseFragment;
import com.example.zyh.toolslib.utils.SPUtils;
import com.google.gson.Gson;
import com.lwb.retrofit.RetrofitTask;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.zsyh.jiujiu.Api;
import com.zsyh.jiujiu.R;
import com.zsyh.jiujiu.bean.GoodBean;
import com.zsyh.jiujiu.home.GoodinfoActivity;
import com.zsyh.jiujiu.net.MyFragementObserver;
import com.zsyh.jiujiu.weight.RecycleGridDivider;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import retrofit2.http.Query;

/**
 * @ProjectName: livvehelp
 * @Package: com.zsyh.lsb.ui
 * @ClassName:
 * @Description: java类作用描述
 * @Author: zsyh
 * @CreateDate: 2020/9/7 11:59
 * @UpdateUser: liangwanbo
 * @UpdateDate: 2020/9/7 11:59
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class GoodFragment extends BaseFragment {

    @BindView(R.id.goodlist)
    RecyclerView goodlist;
    @BindView(R.id.refresh)
    SmartRefreshLayout refresh;
    private String typeid;

    private BaseQuickAdapter<GoodBean.DataBean, BaseViewHolder> mAdapter;
    private List<GoodBean.DataBean> list = new ArrayList<>();

    int pageNumber=1;//页数
    String pageSize="10";//	每页条数
    String typeId;//分类一级id
    String cityId;//城市id
    String userGroup;//企业清库时（userGruop=企业）其余不传
    String isNew;//换限制时（isNew=闲置）其余不传
    String startPrice;//价格区间 开始
    String endPrice;//价格区间 结束
    String name;//搜索关键词
    String userType;//用户等级 传数字 1注册用户 2包月用户 3包年用户
    String classType;//换产品 classType=产品 换服务 classType=服务
    String newType;//新旧程度 全新，九成新，八成新，七成新，七成新以下
    String lat;//	纬度 必传
    String lon;//经度 必传
    String sort;//	价格排序 1价格升序 2价格降序

    public GoodFragment(String typeid) {
        this.typeid = typeid;
        typeId=typeid;
    }


    @Override
    protected int initLayout() {
        return R.layout.fragment_good;
    }

    @Override
    protected void initView(View rootView) {

    }

    @Override
    protected void initData() {
        lat=(String) SPUtils.get(getActivity(), "lat", "");
        lon=(String) SPUtils.get(getActivity(), "lon", "");
        refresh.setEnableRefresh(false);
        refresh.setEnableAutoLoadMore(true);//启用加载
        mAdapter = new BaseQuickAdapter<GoodBean.DataBean, BaseViewHolder>(R.layout.item_goods, list) {
            @Override
            protected void convert(@NonNull BaseViewHolder helper, GoodBean.DataBean item) {
                helper.setText(R.id.describe,item.getGoodsTitle());
                helper.setText(R.id.money,item.getGoodsPrice()+"");
                helper.setText(R.id.city,item.getReleaseCityname());
                helper.setText(R.id.huangood,item.getExchangeClass()+","+item.getExchangeGoods());
                String[] split =item.getGoodsPic().split("\\|");
                Glide.with(getActivity()).load(split[0]).into(helper.<ImageView>getView(R.id.image_good));
                helper.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Bundle bundlehuan = new Bundle();
                        bundlehuan.putString("id", item.getId());
                        ActivityUtils.startActivity(bundlehuan, GoodinfoActivity.class);
                    }
                });
            }
        };

        goodlist.setLayoutManager(new GridLayoutManager(getActivity(), 2));
        goodlist.addItemDecoration(new RecycleGridDivider());
        goodlist.setAdapter(mAdapter);
        GetGood(pageNumber+"", pageSize, typeId, cityId, userGroup, isNew, startPrice, endPrice, name, userType, classType, newType, lat, lon, sort);
        refresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                pageNumber=pageNumber+1;
                GetGood(pageNumber+"", pageSize, typeId, cityId, userGroup, isNew, startPrice, endPrice, name, userType, classType, newType, lat, lon, sort);
            }
        });

    }

    public void GetGood(String pageNumber, String pageSize, String typeId, String cityId, String userGroup, String isNew, String startPrice, String endPrice, String name, String userType, String classType, String newType, String lat, String lon, String sort) {
        RetrofitTask.getInstance()
                .createApi(Api.class)
                .getGoodsList(pageNumber, pageSize, typeId, cityId, userGroup, isNew, startPrice, endPrice, name, userType, classType, newType, lat, lon, sort)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new MyFragementObserver(getActivity(), true) {
                    @Override
                    protected void onError(String errorMsg) {

                    }

                    @Override
                    protected void onSuccess(String JsonStr) {
                        Log.e("good",JsonStr+"");
                        Gson gson=new Gson();
                        GoodBean goodBeans=gson.fromJson(JsonStr,GoodBean.class);
                        for(int i=0;i<goodBeans.getData().size();i++){
                            list.add(goodBeans.getData().get(i));
                        }

                        mAdapter.notifyDataSetChanged();


                    }
                });
    }

}
